<?php

/**
 * This is the model class for table "entry".
 *
 * The followings are the available columns in table 'entry':
 * @property integer $id
 * @property string $code
 * @property integer $client_id
 * @property integer $port_id
 * @property integer $shipping_line_id
 * @property string $customs_entry_no
 * @property string $lodge_date
 * @property string $ETA
 * @property string $selectivity
 * @property string $section
 * @property string $arrastre
 * @property string $wharfage
 * @property string $container_deposit
 * @property string $freight
 * @property string $bill_of_lading_no
 * @property string $final_assessment
 *
 * The followings are the available model relations:
 * @property Container[] $containers
 * @property EmployeeTask[] $employeeTasks
 * @property Client $client
 * @property Port $port
 * @property ShippingLine $shippingLine
 * @property EntryLog[] $entryLogs
 */
class Entry extends CActiveRecord
{



 public function validatecode()
        { if($this->isNewRecord){

		if($this->exists('code=:code',array('code'=>$this->code)))
			{
				$this->addError('code','Code already exists.');
			 }
			 
			 }
        }
	
	/**
	 * @return string the associated database table name
	 */
	 
	 
	 
	public function tableName()
	{
		return 'entry';
	}

	
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		array('code', 'validatecode'),
			array('code, client_id, exporter_address, exporter_name, exporter_tin, port_id, shipping_line_id, manifest_no, country_of_origin, ETA, terms_of_delivery, item, container_size', 'required'),
			array('client_id, port_id, shipping_line_id, insurance, status_id', 'numerical', 'integerOnly'=>true),
			array('code, customs_entry_no, bill_of_lading_no, selectivity, section', 'length', 'max'=>45),
			array('exporter_address', 'length', 'max'=>250),
			array('exporter_name, vessel_aircraft', 'length', 'max'=>70),
			array('exporter_tin, manifest_no, item', 'length', 'max'=>30),
			array('country_of_export, country_of_origin, terms_of_delivery, container_size', 'length', 'max'=>40),
			array('arrastre, wharfage, freight, container_deposit, lodge_date, final_assement_payment_date, date', 'length', 'max'=>10),	
			array('final_assessment', 'length', 'max'=>15),
			array('bank_code', 'length', 'max'=>20),
			array('terms_of_payment, bank_name, bank_branch, bank_reference_no', 'length', 'max'=>50),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('date, code, client_id,  port_id, shipping_line_id, customs_entry_no,  ETA,  bill_of_lading_no,  selectivity, section, final_assement_payment_date, status_id, items, container_size', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		
		  
            "items"=>array(self::MANY_MANY, 'Items',
            'entry_items(entry_id, id)'),
			
			"container"=>array(self::MANY_MANY, 'Container',
            'entry_container(entry_id, id)'),
    
		
		
			'container' => array(self::HAS_MANY, 'Container', 'entry_id'),
			'items' => array(self::HAS_MANY, 'Items', 'entry_id'),
			'employeeTasks' => array(self::HAS_MANY, 'EmployeeTask', 'entry_id'),
			//'client' => array(self::BELONGS_TO, 'Client', 'client_id'),
			 'client' => array(self::BELONGS_TO, 'Client', 'client_id'),
			'port' => array(self::BELONGS_TO, 'Port', 'port_id'),
			'shippingLine' => array(self::BELONGS_TO, 'ShippingLine', 'shipping_line_id'),
			'entryLogs' => array(self::HAS_MANY, 'EntryLog', 'entry_id'),
			'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'code' => 'PRO',
			'status_id' => 'Status',
			'client_id' => 'Client',
			'exporter_address' => 'Exporter Address',
			'exporter_name' => 'Exporter Name',
			'exporter_tin' => 'Exporter Tin',
			'country_of_export' =>'Country Of Export',
			'manifest_no' => 'Manifest No',
			'country_of_origin' => 'Country Of Origin',
			'port_id' => 'Port',
			'shipping_line_id' => 'Shipping Line',
			'customs_entry_no' => 'Customs Entry No',
			'lodge_date' => 'Lodge Date',
			'date' => 'Date',
			'ETA' => 'ETA',
			'selectivity' => 'Selectivity',
			'vessel_aircraft' => 'Vessel/Aircraft',
			'section' => 'Section',
			'insurance' => 'Insurance',
			'arrastre' => 'Arrastre',
			'wharfage' => 'Wharfage',
			'container_deposit' => 'Container Deposit',
			'freight' => 'Freight',
			'bill_of_lading_no' => 'BL. No',
			'final_assessment' => 'Final Assessment',
			'date' => 'Date',
			'bank_code' => 'Bank Code',
			'terms_of_delivery'=>'Terms Of Delivery',
			'terms_of_payment' => 'Terms Of Payment',
			'bank_name' => 'Bank Name',
			'bank_branch' => 'Bank Branch',
			'bank_reference_no' => 'Bank Reference No',
			'lodge_date' => 'Lodge Date',
			'selectivity' => 'Selectivity',
			'section' => 'Section',
			'final_assement_payment_date' => 'Payment Date',
			'status_id' => 'Status',
			'item' => 'Items',
			'container_size' => 'Container Size',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		/*(array(
			'order'=>'date DESC',
		));*/

                $criteria->compare('client.name',$this->client_id, true);
                $criteria->compare('port.name',$this->port_id, true);
                $criteria->compare('shippingLine.name',$this->shipping_line_id, true);
				 $criteria->compare('status.description',$this->status_id, true);
                
                
                $criteria->with=array('client', 'port', 'shippingLine', 'status', 'container');
                
					$criteria->compare('t.code',$this->code, true);
					$criteria->compare('t.date',$this->date, true);
					$criteria->compare('t.customs_entry_no',$this->customs_entry_no,true);
					$criteria->compare('t.bill_of_lading_no',$this->bill_of_lading_no,true);
					$criteria->compare('t.section',$this->section,true);
					$criteria->compare('t.lodge_date',$this->lodge_date,true);
					$criteria->compare('t.ETA',$this->ETA,true);
					$criteria->compare('t.selectivity',$this->selectivity,true);
					$criteria->compare('t.item',$this->item,true);
					$criteria->compare('t.container_size',$this->container_size,true);
					$criteria->compare('t.final_assement_payment_date',$this->final_assement_payment_date,true);
		
		//$criteria->together = true;

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
			'defaultOrder'=>array('id'=>true))
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Entry the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
	